﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NVelocity;
using NVelocity.App;

namespace CARMSBusiness
{
   public class TemplateBusiness
    {
       /// <summary>
       /// get template from string
       ///
       /// </summary>
       /// <param name="templateContent"></param>
       /// <param name="parameters"></param>
       /// <returns></returns>
        public static string GenerateFromTemplate(string templateContent, List<TemplateItem> parameters)
        {
            VelocityEngine vltEngine = new VelocityEngine();
            vltEngine.Init();

            VelocityContext vltContext = new VelocityContext();
            foreach (var item in parameters)
            {
                vltContext.Put(item.Name, item.Value);
            }

            System.IO.StringWriter vltWriter = new System.IO.StringWriter();
            vltEngine.Evaluate(vltContext, vltWriter, null, templateContent);
            return vltWriter.GetStringBuilder().ToString();
        }
    }

    public class TemplateItem
    {
        public string Name { get; set; }
        public object Value { get; set; }
    }

}
